package com.lin.mybatis.jsql;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.update.Update;
import org.junit.jupiter.api.Test;

import java.io.StringReader;

/**
 * @author linjiang
 * @date 2024/6/11
 * @fn
 */
public class TestJSqlBasic {
    // 创建sql解析器管理
    CCJSqlParserManager parserManager = new CCJSqlParserManager();

    @Test
    public void testInsert() throws JSQLParserException {
        String sql = "insert into student(id,name,age) values(1,'林江',24)";
        // 通过sql解析器解析sql语句，最后返回类型。
        Statement statement = parserManager.parse(new StringReader(sql));
        if (statement instanceof Insert) {
            Insert insert = (Insert) statement;
            System.out.println(insert);
        }
    }

    @Test
    public void testSelect() throws JSQLParserException {
        String sql = "select * from student";
        Statement statement = parserManager.parse(new StringReader(sql));
        if (statement instanceof Select) {
            Select select = (Select) statement;
            System.out.println(select);
        }
    }

    @Test
    public void testSelectAndWhere() throws JSQLParserException {
        String sql = "select * from student where 1 = 1";
        Statement statement = parserManager.parse(new StringReader(sql));
        if (statement instanceof Select) {
            Select select = (Select) statement;
            System.out.println(select);
        }
    }

    @Test
    public void testDelete() throws JSQLParserException {
        String sql = "delete from student where id = 1";
        Statement statement = parserManager.parse(new StringReader(sql));
        if (statement instanceof Delete) {
            Delete delete = (Delete) statement;
            System.out.println(delete);
        }
    }

    @Test
    public void testUpdate() throws JSQLParserException {
        String sql = "update student set name = '林仙',age = 18 where id = 1";
        Statement statement = parserManager.parse(new StringReader(sql));
        if (statement instanceof Update) {
            Update update = (Update) statement;
            System.out.println(update);
        }
    }
}
